import {
  Inject,
  Controller,
  Get,
  Post,
  Body,
  Query,
} from '@midwayjs/decorator';

import { Context } from '@midwayjs/koa';
import { GroupService } from '../service/group.service';

@Controller('api')
export class GroupController {
  @Inject()
  ctx: Context;

  @Inject()
  groupService: GroupService;

  /**
   * 建群
   */
  @Post('/createGroup')
  async createGroup(
    @Body('name') name: string,
    @Body('usersId') usersId: string
  ): Promise<any> {
    const result = await this.groupService.createGroup(name, usersId);
    return result;
  }
  /**
   * 加入群聊
   */
  @Post('/joinGroup')
  async joinGroup(
    @Body('groupId') groupId: number,
    @Body('usersId') usersId: string
  ): Promise<any> {
    const result = await this.groupService.joinGroup(groupId, usersId);
    return result;
  }
  /**
   * 获取群列表
   */
  @Get('/getGroupList')
  async getGroupList(): Promise<any> {
    const result = await this.groupService.getGroupList();
    return result;
  }

  /**
   * 获取群详情
   */
  @Get('/getGroupDetails')
  async getGroupDetails(@Query('id') id: number): Promise<any> {
    const result = await this.groupService.getGroupDetails(id);
    return result;
  }

  /**
   * 修改群信息
   */
  @Post('/updateGroup')
  async updateGroup(
    @Body('groupId') groupId: number,
    @Body('name') name: string,
    @Body('imgUrl') imgUrl: string,
    @Body('note') note: string,
    @Body('notice') notice: string
  ): Promise<any> {
    const params = { groupId, name, imgUrl, note, notice };
    const result = await this.groupService.updateGroup(params);
    return result;
  }

  /**
   * 获取群消息
   */
  @Get('/getGroupMsg')
  async getGroupMsg(@Query('groupId') groupId: number): Promise<any> {
    const result = await this.groupService.getGroupMsg(groupId);
    return result;
  }

  /**
   * 获取群成员列表
   */
  @Get('/getGroupMemberList')
  async getGroupMemberList(@Query('id') id: number): Promise<any> {
    const result = await this.groupService.getGroupMemberList(id);
    return result;
  }

  /**
   * 删除群
   */
  @Post('/deleteGroup')
  async deleteGroup(@Body('groupId') groupId: number): Promise<any> {
    const result = await this.groupService.deleteGroup(groupId);
    return result;
  }

  /**
   * 退出群聊
   */
  @Post('/outGroup')
  async outGroup(@Body('groupId') groupId: number): Promise<any> {
    const result = await this.groupService.outGroup(groupId);
    return result;
  }
}
